﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QN_DiemSV.Forms
{
    public partial class frmLop : Form
    {
        public frmLop()
        {
            InitializeComponent();
        }
        bool them = false;
        bool sua = false;
        Classes.connect conn = new QN_DiemSV.Classes.connect();
        void VisionControl()
        {
            txtTenlop.Enabled = true;
            txtMaLop.Enabled = true;
            txtSoSV.Enabled = true;
            cboKhoa.Enabled = true;
        }
        void LoadToCombobox()
        {
            DataTable dt = conn.excuteSelect("select MaKhoa from KHOA");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                cboKhoa.Items.Add(dt.Rows[i][0].ToString());
            }
        }
        void LoadData()
        {
            DataTable dt = new DataTable();
            dt = conn.excuteSelect("select * from LOP");
            dgrLop.DataSource = dt;
        }

        private void frmLop_Load(object sender, EventArgs e)
        {
            txtMaLop.Enabled = false;
            txtSoSV.Enabled = false;
            txtTenlop.Enabled = false;
            cboKhoa.Enabled = false;
            LoadData();
            DataTable dt = conn.excuteSelect("select MaKhoa from KHOA");
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            them = true;
            VisionControl();
            txtMaLop.Focus();
            LoadToCombobox();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            sua = true;
            VisionControl();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (them == true)
            {
                conn.EXE_PROC_PARAMS("LopInsert", "@MaLop", int.Parse(txtMaLop.Text), "@MaK", cboKhoa.SelectedItem.ToString(), "@TenLop", txtTenlop.Text, "@SoSV", int.Parse(txtSoSV.Text));
                LoadData();
            }
            if (sua == true)
            {
                conn.excuteNonquery("update LOP set TenLop ='" + txtTenlop.Text + "', SoSinhVien=" + Int32.Parse(txtSoSV.Text) + ", MaKhoa='" + cboKhoa.Text + "' where MaLop="+ int.Parse(txtMaLop.Text.Trim()));
                LoadData();
            }
        }

        private void dgrLop_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            LoadToCombobox();
            int index = e.RowIndex;
            txtMaLop.Text = dgrLop.Rows[index].Cells["MaLop"].Value.ToString();
            txtSoSV.Text = dgrLop.Rows[index].Cells["SoSinhVien"].Value.ToString();
            txtTenlop.Text = dgrLop.Rows[index].Cells["TenLop"].Value.ToString();
            cboKhoa.Text = dgrLop.Rows[index].Cells["MaKhoa"].Value.ToString();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            conn.excuteNonquery("delete from LOP where MaLop='" + txtMaLop.Text + "'");
            LoadData();
        }
    }
}
